package lambda04;

import org.junit.Test;

import java.lang.reflect.Method;

public class LambdaDemo06 {
    @Test
    public void test02(){
        method02(3,12,(int a,int b)->{return  a+b;});
    }

    public void method02(int a, int b, Calculator c){
        System.out.println(c.getSum(a,b));
    }

    @Test
    public void test03() throws NoSuchMethodException {
        Calculator c = (a,b)-> a+b;
        System.out.println(c);
        System.out.println(c.getClass());
        for (Class<?> in : c.getClass().getInterfaces()) {
            System.out.println(in);
        }

        Method method = c.getClass().getMethod("getSum", int.class, int.class);
        System.out.println(method);
    }

    @Test
    public void test04() throws NoSuchMethodException {
        Calculator c = (a,b)-> a+b;
                        //相当于Calculator c=new LambdaDemo06$$Lambda$1();//多态

        System.out.println(c.getSum(15, 12));   // 27
        //(a,b)-> a+b.getSum(15,17);
        // 如果这样写,Lambda表达式就无法推断这个getSum(int,int)方法到底哪个接口中的方法
    }
}

/*
   //在运行时,JVM会为这个Lambda表达式对应的接口创建一个实现类
   //重写该接口中的抽象方法,重写的目标对应Lambda表达式{}中内容
   //该实现类是一个内部类
   public class LambdaDemo06 {
      @Test
      public void test02() {
         method02(3,12,new LambdaDemo06$$Lambda$1());
      }

      public void method02(int a, int b, Calculator c) {//int a=3,int b=12
                                                        //Calculator c=new LambdaDemo06$$Lambda$1();//多态
         int sum = c.getSum(a, b); //int sum=15;
         System.out.println(sum);
      }
      //1.在运行时,JVM会为这个Lambda表达式对应的接口创建一个实现类
        class LambdaDemo06$$Lambda$1 implements Calculator{
           //2.重写该接口中的抽象方法,重写的目标对应Lambda表达式{}中内容
           public int getSum(int a,int b){  //int a=3,int b=12
               return a+b;
           }
        }

   }
 */